package com.have.blog.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 检查登录账号的类型
 */
public class AccountCheckUtils {
    private static final String CHECK_EMAIL_REGEX = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    private static final String CHECK_MOBILE_NUMBER_REGEX = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    public static boolean isEmail(String query) {
        return validate(CHECK_EMAIL_REGEX, query);
    }

    public static boolean isPhoneNumber(String query) {
        return validate(CHECK_MOBILE_NUMBER_REGEX, query);
    }

    private static boolean validate(String pattern, String query) {
        boolean flag;
        try {
            Pattern regex = Pattern.compile(pattern);
            Matcher matcher = regex.matcher(query);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
}


